package com.baijiayun.liveuibase.widgets.setting;

import com.baijiayun.livecore.context.LPConstants;
import com.baijiayun.livecore.context.LPError;
import com.baijiayun.livecore.context.LiveRoom;
import com.baijiayun.livecore.models.LPBleDevice;
import com.baijiayun.livecore.models.launch.LPEnterRoomNative;
import com.baijiayun.livecore.ppt.PPTView;
import com.baijiayun.livecore.wrapper.LPPlayer;
import com.baijiayun.livecore.wrapper.LPRecorder;
import com.baijiayun.liveuibase.base.RouterListener;
import com.baijiayun.liveuibase.utils.RxUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import android_serialport_api.DeviceInfo;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * @author wubanglin
 */
public abstract class BaseSettingPresenter implements BaseSettingContract.BaseSettingPresenter {
    private LiveRoom liveRoom;
    private LPRecorder recorder;
    private LPPlayer player;
    private BaseSettingContract.BaseSettingView view;
    private ArrayList<String> tcpCdnTag;

    public BaseSettingPresenter(BaseSettingContract.BaseSettingView view) {
        this.view = view;
    }

    @Override
    public LiveRoom getLiveRoom() {
        return liveRoom;
    }

    @Override
    public String getRoomId() {
        return String.valueOf(liveRoom.getRoomId());
    }

    @Override
    public void subscribe(LiveRoom liveRoom) {
        this.liveRoom = liveRoom;
        this.recorder = liveRoom.getRecorder();
        this.player = liveRoom.getPlayer();
    }

    @Override
    public void setRouter(RouterListener routerListener) {

    }

    @Override
    public LPEnterRoomNative.LPPartnerConfig getPartnerConfig() {
        return liveRoom.getPartnerConfig();
    }

    @Override
    public int getWebrtcType() {
        return liveRoom.getRoomInfo().webRTCType;
    }

    @Override
    public boolean isClassStart() {
        return liveRoom.isClassStarted();
    }

    @Override
    public List<LPBleDevice> getRecentBleDevices() {
        return liveRoom.getZXYBVM().getRecentBleDevices();
    }

    @Override
    public DeviceInfo getConnectedDevice() {
        return liveRoom.getZXYBVM().getConnectedDevice();
    }

    @Override
    public void changeCamera() {
        if (liveRoom.getRoomMediaType() == LPConstants.LPMediaType.Audio) {
            return;
        }
        switch (liveRoom.getCurrentUser().getType()) {
            case Teacher:
            case Assistant:
                if (!recorder.isPublishing()) {
                    recorder.publish();
                }
                if (recorder.isVideoAttached()) {
                    view.showCameraSwitchStatus(!recorder.isVideoAttached());
                    detachLocalVideo();
                } else {
                    view.showCameraSwitchStatus(!recorder.isVideoAttached());
                    attachLocalVideo();
                }
                break;
            case Student:
                if (isStudentSpeakApply()) {
                    return;
                }
                if (recorder.isVideoAttached()) {
                    detachLocalVideo();
                } else {
                    attachLocalVideo();
                }
                view.showCameraSwitchStatus(recorder.isVideoAttached());
                break;
            case Visitor:
                break;
            default:
                break;
        }
    }

    @Override
    public boolean isReplaceCamera() {
        return liveRoom != null && liveRoom.getSpeakQueueVM().isReplacedUser();
    }

    @Override
    public void changeMic() {
        switch (liveRoom.getCurrentUser().getType()) {
            case Teacher:
            case Assistant:
                if (!recorder.isPublishing()) {
                    recorder.publish();
                }
                if (recorder.isAudioAttached()) {
                    recorder.detachAudio();
                } else {
                    attachLocalAudio();
                }
                break;
            case Student:
                if (isStudentSpeakApply()) {
                    return;
                }
                if (recorder.isAudioAttached()) {
                    recorder.detachAudio();
                } else {
                    attachLocalAudio();
                }
                break;
            case Visitor:
                view.showVisitorFail();
                break;
            default:
                break;
        }
    }

    @Override
    public void setRemarksEnable(boolean isEnable) {

    }

    @Override
    public void switchForbidAllAudio(boolean isForbid) {
        liveRoom.requestForbidAllAudio(isForbid);
    }

    @Override
    public boolean switchForbidStatus() {
        if (!canSwitchForbid()) {
            view.showSwitchForbid();
            return false;
        }
        liveRoom.requestForbidAllChat(!liveRoom.getForbidStatus(LPConstants.LPForbidChatType.TYPE_GROUP));
        return true;
    }

    private boolean canSwitchForbid() {
        return !(liveRoom.getCurrentUser().getType() == LPConstants.LPUserType.Assistant && liveRoom.getAdminAuth() != null
                && !liveRoom.getAdminAuth().forbidAndKick);
    }

    @Override
    public void switchForbidRaiseHand() {
        liveRoom.requestForbidRaiseHand(!liveRoom.getForbidRaiseHandStatus());
    }

    @Override
    public void setBeautyLevel(float beautyLevel) {
        liveRoom.getRecorder().setBeautyLevel(beautyLevel);
    }

    @Override
    public float getBeautyLevel() {
        return liveRoom.getRecorder().getBeautyLevel();
    }

    @Override
    public void setWhitenessLevel(float whitenessLevel) {
        liveRoom.getRecorder().setWhitenessLevel(whitenessLevel);
    }

    @Override
    public float getWhitenessLevel() {
        return liveRoom.getRecorder().getWhitenessLevel();
    }

    @Override
    public boolean isVideoAttached() {
        return liveRoom != null && liveRoom.getRecorder().isVideoAttached();
    }

    @Override
    public boolean isTeacherOrAssistant() {
        return liveRoom.isTeacherOrAssistant() || liveRoom.isGroupTeacherOrAssistant();
    }

    @Override
    public boolean isUseWebRTC() {
        return liveRoom.isUseWebRTC();
    }

    @Override
    public boolean isGroup() {
        return liveRoom.getCurrentUser().getGroup() != 0;
    }

    @Override
    public void switchChangePPTEnable(int checkedPosition) {
        if (liveRoom.hasH5PPTAuth() && checkedPosition == 1) {
            view.onChangePPTForbidFailed();
            liveRoom.requestH5PPTAuth(false);
        }
        liveRoom.requestForbidStudentSwitchPPT(checkedPosition == 1);
    }

    @Override
    public void switchAllHorizonMirrorMode(boolean on) {
        liveRoom.getSpeakQueueVM().requestMirrorModeAllSwitch(on, liveRoom.getSpeakQueueVM().getVerticalMirrorModeSet().contains(liveRoom.getCurrentUser().getNumber()));
    }

    @Override
    public void switchAllVerticalMirrorMode(boolean on) {
        liveRoom.getSpeakQueueVM().requestMirrorModeAllSwitch(liveRoom.getSpeakQueueVM().getHorizontalMirrorModeSet().contains(liveRoom.getCurrentUser().getNumber()), on);
    }

    @Override
    public void switchCamera(boolean asFront) {
        if (recorder != null && asFront != recorder.getCameraOrientation()) {
            recorder.switchCamera();
            view.showWhichCamera(recorder.getCameraOrientation());
        }
    }

    @Override
    public boolean getCameraOrientation() {
        return recorder.getCameraOrientation();
    }

    @Override
    public boolean isAudioAttached() {
        return liveRoom != null && liveRoom.getRecorder().isAudioAttached();
    }

    @Override
    public int getSpeakApplyStatus() {
        return 0;
    }

    @Override
    public LPConstants.LPLinkType getLinkType() {
        return recorder.getLinkType();
    }

    @Override
    public boolean getForbidRaiseHandStatus() {
        return liveRoom != null && liveRoom.getForbidRaiseHandStatus();
    }

    @Override
    public boolean getForbidAllAudioStatus() {
        return liveRoom != null && liveRoom.getForbidAllAudioStatus();
    }

    @Override
    public boolean getForbidStatus(LPConstants.LPForbidChatType typeGroup) {
        return liveRoom != null && liveRoom.getForbidStatus(typeGroup);
    }

    @Override
    public boolean getStudentSwitchPPTState() {
        return liveRoom != null && liveRoom.getStudentSwitchPPTState();
    }

    @Override
    public Set<String> getHorizontalMirrorModeSet() {
        return liveRoom.getSpeakQueueVM().getHorizontalMirrorModeSet();
    }

    @Override
    public Set<String> getVerticalMirrorModeSet() {
        return liveRoom.getSpeakQueueVM().getVerticalMirrorModeSet();
    }

    @Override
    public LPConstants.LPResolutionType getVideoDefinition() {
        return liveRoom.getRecorder().getVideoDefinition();
    }

    @Override
    public LPError setCaptureVideoDefinition(LPConstants.LPResolutionType resolutionType) {
        return liveRoom.getRecorder().setCaptureVideoDefinition(resolutionType);
    }

    @Override
    public LPConstants.TemplateType getTemplateType() {
        return liveRoom.getTemplateType();
    }

    @Override
    public LPConstants.LPRoomType getRoomType() {
        return liveRoom.getRoomType();
    }

    @Override
    public int getCDNCount() {
        return tcpCdnTag == null ? 0 : tcpCdnTag.size();
    }

    @Override
    public void setUpCDNLink(int order) {
        if (liveRoom.getRecorder().setTcpWithCdn(tcpCdnTag.get(order))) {
            view.showUpLinkType(true);
        } else {
            view.showSwitchLinkTypeError();
        }
    }

    @Override
    public void setUpLinkTCP() {
        if (recorder.setLinkType(LPConstants.LPLinkType.TCP)) {
            view.showUpLinkType(true);
        } else {
            view.showSwitchLinkTypeError();
        }
    }

    @Override
    public void setUpLinkUDP() {
        if (recorder.setLinkType(LPConstants.LPLinkType.UDP)) {
            view.showUpLinkType(false);
        } else {
            view.showSwitchLinkTypeError();
        }
    }

    @Override
    public void setDownLinkTCP() {
        if (player.setLinkType(LPConstants.LPLinkType.TCP)) {
            view.showDownLinkType(true);
        } else {
            view.showSwitchLinkTypeError();
        }
    }

    @Override
    public void setDownLinkUDP() {
        if (player.setLinkType(LPConstants.LPLinkType.UDP)) {
            view.showDownLinkType(false);
        } else {
            view.showSwitchLinkTypeError();
        }
    }

    @Override
    public void setDownCDNLink(int order) {
        String tcpCdn = "";
        //-1为自动模式，设置preferCDN=""
        if (order >= 0 && order < tcpCdnTag.size()) {
            tcpCdn = tcpCdnTag.get(order);
        }
        if (liveRoom.getPlayer().setLinkTypeTcpWithCdn(tcpCdn)) {
            view.showDownLinkType(true);
        } else {
            view.showSwitchLinkTypeError();
        }
    }

    @Override
    public void subscribe() {
        try {
            tcpCdnTag = (ArrayList<String>) liveRoom.getPartnerConfig().msConfig.get("live_stream_cdn_list");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public LPConstants.LPResolutionType getMaxVideoDefinition() {
        return liveRoom.getRecorder().getMaxVideoDefinition();
    }

    @Override
    public void unSubscribe() {
        
    }

    @Override
    public void destroy() {
    }
}
